var log4js = require('log4js');
var crypto = require('crypto');

process.env.PORT = 80;
!process.env.NODE_ENV && (process.env.NODE_ENV = 'development');
!process.env.NODE_SERVER && (process.env.NODE_SERVER = "localhost");

/**
 * log4js configuration
 */
log4js.configure({
  appenders: [
    { type: 'console' },
    {
      type: 'dateFile',
      pattern: "yyyy-MM-dd.log",
      filename : 'logs/',
      absolute: false,
      alwaysIncludePattern: true,
      category: 'app'
    }
  ],
  replaceConsole: true
});

exports.logger=function(name){
  var logger = log4js.getLogger(name);
  logger.setLevel(process.env.NODE_ENV === 'development' ? 'TRACE':'INFO');
  return logger;
};


/**
 * generate appId and appKey
 * */
var now = Date.now();
var key = 'A6908702422808'+"UZ"+'5AF7369A-D5C6-190A-60D5-7E3376D9BAD6'+"UZ"+now;
var sha1 = crypto.createHash('sha1');

sha1.update(key);
key = sha1.digest('hex');
key+=("."+now);
var apiKey = {id : 'A6908702422808',key : key};

exports.cloud = apiKey;


/**
 *  apiCloud request template.Default HTTP POST
 * */
var reqTemplate = function(model,method){
  this.url ="https://d.apicloud.com/mcm/api/" + model;
  this.method = method || "POST";
  this.headers = {
    "X-APICloud-AppId": apiKey.id,
    "X-APICloud-AppKey": apiKey.key,
    "Content-Type": "application/json"
  };
  this.form = {};
};

exports.reqTemplate = reqTemplate;